const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'build.js',
    path: resolve(__dirname, 'dist'),
    // 需要添加publicPath,影响webpack-dev-server
    // publicPath: './'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.less$/,
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
        // test: /\.(jpg|png|gif)$/,
        test: /\.(jpg|png|gif)$/,
        loader: 'url-loader',
        options: {
          // 图片大小于小8KB，就会被base64处理
          limit: 8 * 1024,
          esModule: false,
          name: '[hash:10].[ext]'
        }
      },
      {
        // url-loader以es6模块化来解析图片，而html-loader以commonjs模块规范来解析
        test: /\.html$/,
        // 处理html的image图片
        loader: 'html-loader'
      },
      {
        exclude: /\.(css|js|html|less|json|jpg|png)$/,
        loader: 'file-loader',
        options: {
          name: '[hash:10].[ext]'
        }
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ],
  mode: 'development',
  // mode:'production'
  // 起一个server，只会在内存中编译打包，不会有输出
  // 启动指令 webpack-dev-server
  devServer: {
    contentBase: resolve(__dirname, 'dist'),
    compress: true,
    port: 3000,
    open: true
  }
}
